#define BLINKER_WIFI
#include <Blinker.h>


char auth[] = "e5c3e51e5242";
char ssid[] = "XC";
char pswd[] = "XZB123456";

BlinkerButton Button1("BUTTON_1");
BlinkerButton Button2("BUTTON_2");
BlinkerButton Button3("BUTTON_3");
BlinkerButton Button4("BUTTON_4");
BlinkerButton Button5("BUTTON_5");
BlinkerButton Button6("BUTTON_6");
BlinkerButton Button7("W");
BlinkerButton Button8("S");
BlinkerButton Button9("A");
BlinkerButton Button10("D");
BlinkerButton Button11("WA");
BlinkerButton Button12("DS");
BlinkerButton Button13("WD");
BlinkerButton Button14("AS");
BlinkerButton Button15("STOP");

//定义蓝牙，按钮，摇杆

int FRONT_LEFT_EN = 16;
int FRONT_LEFT_IN1 = 14;
int FRONT_LEFT_IN2 = 12;
int FRONT_RIGHT_EN = 17;
int FRONT_RIGHT_IN3 = 13;
int FRONT_RIGHT_IN4 = 15;
int REAR_LEFT_EN = 1;
int REAR_LEFT_IN1 = 3;
int REAR_LEFT_IN2 = 5;
int REAR_RIGHT_EN = 2;
int REAR_RIGHT_IN3 = 4;
int REAR_RIGHT_IN4 = 0;




int speed;
void Button1_callback(const String & state)
{
   if(state == "tap")
   {
     speed = 64;
   }
}
void Button2_callback(const String & state)
{
    if(state == "tap")
   {
    speed = 128;
   } 
}
void Button3_callback(const String & state)
{
    if(state == "tap")
   {
    speed = 255;
   }
}
//BUTTON123控制电机速度



void Button4_callback(const String & state)
{
   if(state == "tap")
   {
       // 前左电机
  digitalWrite(FRONT_LEFT_IN1, LOW);
  digitalWrite(FRONT_LEFT_IN2, HIGH);
  analogWrite(FRONT_LEFT_EN, 255);

  // 前右电机
  digitalWrite(FRONT_RIGHT_IN3, HIGH);
  digitalWrite(FRONT_RIGHT_IN4, LOW);
  analogWrite(FRONT_RIGHT_EN, 255);

  // 后左电机
  digitalWrite(REAR_LEFT_IN1, LOW);
  digitalWrite(REAR_LEFT_IN2, HIGH);
  analogWrite(REAR_LEFT_EN, 255);

  // 后右电机
  digitalWrite(REAR_RIGHT_IN3, HIGH);
  digitalWrite(REAR_RIGHT_IN4, LOW);
  analogWrite(REAR_RIGHT_EN, 255);
   }
}
void Button5_callback(const String & state)
{
   if(state == "tap")
   {
       // 前左电机
  digitalWrite(FRONT_LEFT_IN1, LOW);
  digitalWrite(FRONT_LEFT_IN2, HIGH);
  analogWrite(FRONT_LEFT_EN, 64);

  // 前右电机
  digitalWrite(FRONT_RIGHT_IN3, HIGH);
  digitalWrite(FRONT_RIGHT_IN4, LOW);
  analogWrite(FRONT_RIGHT_EN, 64);

  // 后左电机
  digitalWrite(REAR_LEFT_IN1, LOW);
  digitalWrite(REAR_LEFT_IN2, HIGH);
  analogWrite(REAR_LEFT_EN, 64);

  // 后右电机
  digitalWrite(REAR_RIGHT_IN3, HIGH);
  digitalWrite(REAR_RIGHT_IN4, LOW);
  analogWrite(REAR_RIGHT_EN, 64);
   }
}
void Button6_callback(const String & state)
{
   if(state == "tap")
   {
       // 前左电机
  digitalWrite(FRONT_LEFT_IN1, HIGH);
  digitalWrite(FRONT_LEFT_IN2, LOW);
  analogWrite(FRONT_LEFT_EN, 64);

  // 前右电机
  digitalWrite(FRONT_RIGHT_IN3, LOW);
  digitalWrite(FRONT_RIGHT_IN4, HIGH);
  analogWrite(FRONT_RIGHT_EN, 64);

  // 后左电机
  digitalWrite(REAR_LEFT_IN1, HIGH);
  digitalWrite(REAR_LEFT_IN2, LOW);
  analogWrite(REAR_LEFT_EN, 64);

  // 后右电机
  digitalWrite(REAR_RIGHT_IN3, LOW);
  digitalWrite(REAR_RIGHT_IN4, HIGH);
  analogWrite(REAR_RIGHT_EN, 64);
   }
}
//BUTTON456控制旋转

void Button7_callback(const String & state)
{
   if(state == "tap")
   {
    FRONT();
   }
}
void Button8_callback(const String & state)
{
   if(state == "tap")
   {
    BEHIND();
   }
}
void Button9_callback(const String & state)
{
   if(state == "tap")
   {
    LEFT();
   }
}
void Button10_callback(const String & state)
{
   if(state == "tap")
   {
    RIGHT();
   }
}
void Button11_callback(const String & state)
{
   if(state == "tap")
   {
    LEFTFRONT();
   }
}
void Button12_callback(const String & state)
{
   if(state == "tap")
   {
    RIGHTBEHIND();
   }
}
void Button13_callback(const String & state)
{
   if(state == "tap")
   {
    RIGHTFRONT();
   }
}
void Button14_callback(const String & state)
{
   if(state == "tap")
   {
    LEFTBEHIND();
   }
}
void Button15_callback(const String & state)
{
   if(state == "tap")
   {
    stopMotors();
   }
}




void FRONT()
{
  // 前左电机
  digitalWrite(FRONT_LEFT_IN1, HIGH);
  digitalWrite(FRONT_LEFT_IN2, LOW);
  analogWrite(FRONT_LEFT_EN, speed);

  // 前右电机
  digitalWrite(FRONT_RIGHT_IN3, HIGH);
  digitalWrite(FRONT_RIGHT_IN4, LOW);
  analogWrite(FRONT_RIGHT_EN, speed);

  // 后左电机
  digitalWrite(REAR_LEFT_IN1, HIGH);
  digitalWrite(REAR_LEFT_IN2, LOW);
  analogWrite(REAR_LEFT_EN, speed);

  // 后右电机
  digitalWrite(REAR_RIGHT_IN3, HIGH);
  digitalWrite(REAR_RIGHT_IN4, LOW);
  analogWrite(REAR_RIGHT_EN, speed);
}
void BEHIND()
{
  // 前左电机
  digitalWrite(FRONT_LEFT_IN1, LOW);
  digitalWrite(FRONT_LEFT_IN2, HIGH);
  analogWrite(FRONT_LEFT_EN, speed);

  // 前右电机
  digitalWrite(FRONT_RIGHT_IN3, LOW);
  digitalWrite(FRONT_RIGHT_IN4, HIGH);
  analogWrite(FRONT_RIGHT_EN, speed);

  // 后左电机
  digitalWrite(REAR_LEFT_IN1, LOW);
  digitalWrite(REAR_LEFT_IN2, HIGH);
  analogWrite(REAR_LEFT_EN, speed);

  // 后右电机
  digitalWrite(REAR_RIGHT_IN3, LOW);
  digitalWrite(REAR_RIGHT_IN4, HIGH);
  analogWrite(REAR_RIGHT_EN, speed);
}
void LEFT()
{
  // 前左电机
  digitalWrite(FRONT_LEFT_IN1, LOW);
  digitalWrite(FRONT_LEFT_IN2, HIGH);
  analogWrite(FRONT_LEFT_EN, speed);

  // 前右电机
  digitalWrite(FRONT_RIGHT_IN3, HIGH);
  digitalWrite(FRONT_RIGHT_IN4, LOW);
  analogWrite(FRONT_RIGHT_EN, speed);

  // 后左电机
  digitalWrite(REAR_LEFT_IN1, HIGH);
  digitalWrite(REAR_LEFT_IN2, LOW);
  analogWrite(REAR_LEFT_EN, speed);

  // 后右电机
  digitalWrite(REAR_RIGHT_IN3, LOW);
  digitalWrite(REAR_RIGHT_IN4, HIGH);
  analogWrite(REAR_RIGHT_EN, speed);
}
void RIGHT()
{
  // 前左电机
  digitalWrite(FRONT_LEFT_IN1, HIGH);
  digitalWrite(FRONT_LEFT_IN2, LOW);
  analogWrite(FRONT_LEFT_EN, speed);

  // 前右电机
  digitalWrite(FRONT_RIGHT_IN3, LOW);
  digitalWrite(FRONT_RIGHT_IN4, HIGH);
  analogWrite(FRONT_RIGHT_EN, speed);

  // 后左电机
  digitalWrite(REAR_LEFT_IN1, LOW);
  digitalWrite(REAR_LEFT_IN2, HIGH);
  analogWrite(REAR_LEFT_EN, speed);

  // 后右电机
  digitalWrite(REAR_RIGHT_IN3, HIGH);
  digitalWrite(REAR_RIGHT_IN4, LOW);
  analogWrite(REAR_RIGHT_EN, speed);
}
void stopMotors()
{
  // 前左电机
  analogWrite(FRONT_LEFT_EN, 0);

  // 前右电机
  analogWrite(FRONT_RIGHT_EN, 0);

  // 后左电机
  analogWrite(REAR_LEFT_EN, 0);

  // 后右电机
  analogWrite(REAR_RIGHT_EN, 0);
}
void LEFTFRONT()
{
  // 前左电机
  analogWrite(FRONT_LEFT_EN, 0);

  // 前右电机
  digitalWrite(FRONT_RIGHT_IN3, HIGH);
  digitalWrite(FRONT_RIGHT_IN4, LOW);
  analogWrite(FRONT_RIGHT_EN, speed);

  // 后左电机
  digitalWrite(REAR_LEFT_IN1, HIGH);
  digitalWrite(REAR_LEFT_IN2, LOW);
  analogWrite(REAR_LEFT_EN, speed);

  // 后右电机
  analogWrite(REAR_RIGHT_EN, 0);
}
void RIGHTBEHIND()
{
  // 前左电机
  analogWrite(FRONT_LEFT_EN, speed);

  // 前右电机
  digitalWrite(FRONT_RIGHT_IN3, LOW);
  digitalWrite(FRONT_RIGHT_IN4, HIGH);
  analogWrite(FRONT_RIGHT_EN, speed);

  // 后左电机
  digitalWrite(REAR_LEFT_IN1, LOW);
  digitalWrite(REAR_LEFT_IN2, HIGH);
  analogWrite(REAR_LEFT_EN, speed);

  // 后右电机
  analogWrite(REAR_RIGHT_EN, 0);
}
void RIGHTFRONT()
{
  // 前左电机
  digitalWrite(FRONT_LEFT_IN1, HIGH);
  digitalWrite(FRONT_LEFT_IN2, LOW);
  analogWrite(FRONT_LEFT_EN, speed);

  // 前右电机
  analogWrite(FRONT_RIGHT_EN, 0);

  // 后左电机
  analogWrite(REAR_LEFT_EN, 0);

  // 后右电机
  digitalWrite(REAR_RIGHT_IN3, HIGH);
  digitalWrite(REAR_RIGHT_IN4, LOW);
  analogWrite(REAR_RIGHT_EN, speed);
}
void LEFTBEHIND()
{
  // 前左电机
  digitalWrite(FRONT_LEFT_IN1, LOW);
  digitalWrite(FRONT_LEFT_IN2, HIGH);
  analogWrite(FRONT_LEFT_EN, speed);

  // 前右电机
  analogWrite(FRONT_RIGHT_EN, 0);

  // 后左电机
  analogWrite(REAR_LEFT_EN, 0);

  // 后右电机
  digitalWrite(REAR_RIGHT_IN3, LOW);
  digitalWrite(REAR_RIGHT_IN4, HIGH);
  analogWrite(REAR_RIGHT_EN, speed);
}
//以下的全是方向函数

void setup() 
{
  // 设置所有引脚为输出模式
  pinMode(FRONT_LEFT_IN1, OUTPUT);
  pinMode(FRONT_LEFT_IN2, OUTPUT);
  pinMode(FRONT_LEFT_EN, OUTPUT);
  pinMode(FRONT_RIGHT_IN3, OUTPUT);
  pinMode(FRONT_RIGHT_IN4, OUTPUT);
  pinMode(FRONT_RIGHT_EN, OUTPUT);
  pinMode(REAR_LEFT_IN1, OUTPUT);
  pinMode(REAR_LEFT_IN2, OUTPUT);
  pinMode(REAR_LEFT_EN, OUTPUT);
  pinMode(REAR_RIGHT_IN3, OUTPUT);
  pinMode(REAR_RIGHT_IN4, OUTPUT);
  pinMode(REAR_RIGHT_EN, OUTPUT);

  // 初始化Blinker
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);
    BLINKER_DEBUG.debugAll();
  Blinker.begin(auth, ssid, pswd);
  Button1.attach(Button1_callback);
  Button2.attach(Button2_callback);
  Button3.attach(Button3_callback);
  Button4.attach(Button4_callback);
  Button5.attach(Button5_callback);
  Button6.attach(Button6_callback);
  Button7.attach(Button7_callback);
  Button8.attach(Button8_callback);
  Button9.attach(Button9_callback);
  Button10.attach(Button10_callback);
  Button11.attach(Button11_callback);
  Button12.attach(Button12_callback);
  Button13.attach(Button13_callback);
  Button14.attach(Button14_callback);
  Button15.attach(Button15_callback);
}

void loop() 
{
  // 处理Blinker事件
  Blinker.run();
}